package particle_simulator_chem;

import java.io.FileNotFoundException;
import java.util.ArrayList;

import org.eclipse.swt.graphics.Color;

import chemistry.Compound;
import chemistry.Element;
import chemistry.ElementList;

public class CrystalTest {
	static World world;
	static ArrayList<Compound> compounds = new ArrayList<Compound>(0);
	public static void main(String[] args) throws FileNotFoundException {
		world = new World(500, 500, 0, .01, 300, 10000, 10000, 0.1, compounds);

		System.out.println(findBest(world, world.getElements()));
	}
	
	public static Boid formulaBoid(double x, double y, double z, String formula){
		Boid boid = new Boid(x, y, z, 0.0, 0.0, 0.0, new Color(null, 255, 0, 0), formula, formula, world.getElements());
		return boid;
	}
	
	public static String findBest(World world, ElementList elements) {
		
		String bestFormula = "";
		double maxHeight = Double.MAX_VALUE;
		
		for(int i = 0; i < elements.getSize(); i++) {
			String symbol = elements.getElement(i).getSymbol();
			double height = testElement(world, symbol, 100);
			if(height < maxHeight) {
				maxHeight = height;
				bestFormula = symbol;
			}
			System.out.println("(" + symbol + ", " + height + ")");
		}
		
		return bestFormula;
	}
	
	public static double testElement(World world, String formula, int iterations) {
		//add boids
		for(int x = 0; x < 20; x++) {
			for(int y = 0; y < 20; y++) {
				world.addBoid(formulaBoid(x * 5, y * 5, 0, formula));
			}
		}
		
		//update i iterations
		for(int i = 0; i < iterations; i++) {
			world.updateAllBoids();
		}
		
		//find maximum height
		double maxHeight = Double.MAX_VALUE;
		ArrayList<Boid> boids = world.getBoids();
		
		for (Boid boid : boids) {
			double boidY = boid.getLocation().getY();
			if (boidY < maxHeight) {
				maxHeight = boidY;
			}
		}
		//System.out.println(maxHeight);
		return maxHeight;
	}
}
